package netWork;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {

private Socket socket=null; //套接字
    
  public TcpEchoClient(String severIp,int severPort) throws IOException{
      socket=new Socket(severIp,severPort);

  }

  public void start(){
      System.out.println("客户端启动");


      try {
          while (true){

          }
      } catch (Exception e) {
          e.printStackTrace();
      }

      try(InputStream inputStream=socket.getInputStream();
    OutputStream outputStream=socket.getOutputStream()){
    Scanner scanner=new Scanner(inputStream);
    Scanner scannerIn=new Scanner(System.in);
    PrintWriter printWriter=new PrintWriter(outputStream);
    while (true){
 //1.从控制台读取数据
        System.out.println("->");
        String request=scannerIn.next();

        //2.请求发送给服务器
        printWriter.println(request);
        //3.从服务器读取响应
        if (scanner.hasNext()){
            break;
        }
        String response=  scanner.next();
    }
}catch ( Exception e){
    throw new RuntimeException(e);
}
  }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client=new TcpEchoClient("127.0.0.1",9090);
        client.start();
    }


}
